﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace Tank
{
    class Particle
    {
        Vector2 position;
        Vector2 velocity;
        Vector2 acceleration;
        float   drag;
        float   angle;
        float   angVel;
        float   size, startSize, endSize;
        float   totalTime;
        float   currTime;
        float   alphaBlendTime;
        Color   color;
        bool    fadeSize;
        bool    remove;
        string  textureID;

        public Particle(Vector2 pos, Vector2 vel, Vector2 accel, Color clor,
                        float maxSize, float lifeTotal, float alphaTime, float drg,
                        float angVelocity, float blendTime, string textID)
        {
            position = new Vector2(pos.X, pos.Y);
            velocity = new Vector2(vel.X, vel.Y);
            acceleration = new Vector2(accel.X, accel.Y);
            angle = 0;
            angVel = angVelocity;
            size = startSize = endSize = maxSize;
            totalTime = lifeTotal;
            drag = drg;
            currTime = 0;
            color = clor;
            textureID = textID;
            remove = false;
            alphaBlendTime = alphaTime;
            fadeSize = false;
        }

        public void Update(float deltaTime)
        {
            currTime += deltaTime;
            if (currTime >= totalTime)
            {
                remove = true;
                return;
            }

            if (acceleration.X != 0 || acceleration.Y != 0)
            {
                velocity += acceleration;
                acceleration.X = 0; acceleration.Y = 0;
            }

            position += (velocity * deltaTime);
            ApplyDrag();

            angle += angVel * deltaTime;

            // Adjust alpha
            if (currTime > alphaBlendTime)
            {
                color.A = (byte)((1 - (currTime - alphaBlendTime) / (totalTime - alphaBlendTime)) * 255);
            }

            if (fadeSize)
            {
                float lifePerct = 1 - ((totalTime - currTime) / totalTime);
                size = startSize + ((endSize - startSize) * lifePerct);
            }
        }

        public void Draw(SpriteBatch spriteBatch, Rectangle sourceRect)
        {
            

            Texture2D texture = World.Instance.GetTexture(textureID);
            float halfWidth = (float)texture.Width * .5f;
            float halfHeight = (float)texture.Height *.5f;
            Vector2 drawPos = new Vector2(position.X, position.Y);
            spriteBatch.Draw(texture, position, sourceRect, color, angle, new Vector2(halfWidth, halfHeight), size, SpriteEffects.None, 1);
            //spriteBatch.Draw(texture, drawPos, sourceRect, color);
        }

        public void ApplyDrag()
        {
            velocity -= (velocity * drag);
        }

        public void ApplyVelocity(Vector2 _velocity)
        {
            velocity += _velocity;
        }

        public void ApplyAccel(Vector2 accel)
        {
            acceleration += accel;
        }

        public void SetFadeSize(float start, float end)
        {
            startSize = start;
            endSize = end;
            fadeSize = true;
        }

        public bool Remove
        {
            get { return remove; }
        }

    }
}
